package com.yigo.framework.http;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> {
    private String code;
    private String msg;
    private T data;
    private RPage page;

    private R(ErrorCode errorCode) {
        this(errorCode, null);
    }

    private R(ErrorCode errorCode, T data) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
        this.data = data;
    }

    public R(RCodeEnum errorCode, T data, RPage page) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
        this.data = data;
        this.page = page;
    }

    private R(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return new R<T>(RCodeEnum.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return new R<T>(RCodeEnum.SUCCESS, data);
    }

    public static <T> R<T> ok(T data, RPage page) {
        return new R<T>(RCodeEnum.SUCCESS, data, page);
    }

    public static <T> R<T> fail() {
        return new R<T>(RCodeEnum.FAIL);
    }

    public static <T> R<T> fail(ErrorCode errorCode) {
        return new R<T>(errorCode);
    }

    public static R fail(ErrorCode errorCode, Object... args) {
        return new R(errorCode.getCode(), errorCode.getMsg(args), null);
    }
}
